<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="用户名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="手机号码" prop="userPhone">
        <el-input
          v-model="queryParams.userPhone"
          placeholder="请输入手机号码"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="流水号" prop="orderSn">
        <el-input
          v-model="queryParams.orderSn"
          placeholder="请输入流水号"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="还款方式" prop="type">
      <el-select
        v-model="queryParams.type"
        placeholder="还款方式"
        clearable
        style="width: 240px"
      >
        <el-option
          v-for="item in typeList"
          :key="item.type"
          :label="item.value"
          :value="item.type"
        />
      </el-select>
      </el-form-item>

      <el-form-item label="还款时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="paymentLogList">
      <el-table-column label="平台" align="center" prop="companyId"/>
      <el-table-column label="用户名" align="center" prop="userName"/>
      <el-table-column label="手机号码" align="center" prop="userPhone"/>
      <el-table-column label="订单号" align="center" prop="orderSn"/>
      <el-table-column label="还款金额" align="center" prop="repaymentMoney"/>
      <el-table-column label="还款方式" align="center" prop="type">
        <template slot-scope="scope">
          <span v-if="scope.row.type == '1'">线上还款</span>
          <span v-else>线下还款</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status == '0'">还款中</span>
          <span v-if="scope.row.status == '1'">还款失败</span>
          <span v-if="scope.row.status == '2'">还款成功</span>
          <span v-if="scope.row.status == '3'">已撤销</span>
        </template>
      </el-table-column>
      <el-table-column label="还款时间" align="center" prop="repaymentTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.repaymentTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="160"
        fixed="right"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status == '2'"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handlerPaymentLog(scope.row)"
          >撤销
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
  import {getPaymentLogList,revokeRepaymentLog} from "@/api/app/apply";
  export default {
    name: "PaymentLog",
    data() {
      return {
        // 遮罩层
        loading: true,
        showSearch: true,
        // 总条数
        total: 0,
        // 订单表格数据
        paymentLogList: null,
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 日期范围
        dateRange: [],
        // 表单参数
        form: {},
        typeList: [
          {
            type: '1',
            value: '线上还款'
          },
          {
            type: '0',
            value: '线下还款'
          },
        ],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          userName: undefined,
          type: undefined,
          userPhone: undefined,
          userId: this.$route.params.userId
        },
      };
    },
    watch: {},
    created() {
      this.getList();
    },
    methods: {
      /** 查询列表 */
      getList() {
        this.loading = true;
        getPaymentLogList(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
            this.paymentLogList = response.rows;
            this.total = response.total;
            this.loading = false;
          }
        );
      },

      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },

      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },

      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },

      handlerPaymentLog(row){
        this.$modal.confirm('是否确认对该账单撤销？').then(function () {
          return revokeRepaymentLog(row.id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("操作成功");
        }).catch(() => {
        });
      }

    }
  };
</script>
